home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
285_01
/
bisoninf.3
< prev
next >
Wrap
Text File
|
1990-07-08
|
50KB
|
1,335 lines
Info file bison.info, produced by Makeinfo, -*- Text -*- from input
file bison.texinfo.
This file documents the Bison parser generator.
Copyright (C) 1988 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled ``Bison General Public License'' and
``Conditions for Using Bison'' are included exactly as in the
original, and provided that the entire resulting derived work is
distributed under the terms of a permission notice identical to this
one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the text of the translations of the sections
entitled ``Bison General Public License'' and ``Conditions for Using
Bison'' must be approved for accuracy by the Foundation.
File: bison.info, Node: Action Features, Prev: Error Reporting, Up: Interface
Special Features for Use in Actions
===================================
Here is a table of Bison constructs, variables and macros that are
useful in actions.
`$$'
Acts like a variable that contains the semantic value for the
grouping made by the current rule. *Note Actions::.
`$N'
Acts like a variable that contains the semantic value for the
Nth component of the current rule. *Note Actions::.
`$<TYPEALT>$'
Like `$$' but specifies alternative TYPEALT in the union
specified by the `%union' declaration. *Note Action Types::.
`$<TYPEALT>N'
Like `$N' but specifies alternative TYPEALT in the union
specified by the `%union' declaration. *Note Action Types::.
`YYABORT;'
Return immediately from `yyparse', indicating failure. *Note
Parser Function::.
`YYACCEPT;'
Return immediately from `yyparse', indicating success. *Note
Parser Function::.
`YYEMPTY'
Value stored in `yychar' when there is no look-ahead token.
`YYERROR;'
Cause an immediate syntax error. This causes `yyerror' to be
called, and then error recovery begins. *Note Error Recovery::.
`yychar'
Variable containing the current look-ahead token. When there is
no look-ahead token, the value `YYERROR' is stored here. *Note
Look-Ahead::.
`yyclearin;'
Discard the current look-ahead token. This is useful primarily
in error rules. *Note Error Recovery::.
`yyerrok;'
Resume generating error messages immediately for subsequent
syntax errors. This is useful primarily in error rules. *Note
Error Recovery::.
`@N'
Acts like a structure variable containing information on the
line numbers and column numbers of the Nth component of the
current rule. The structure has four members, like this:
struct {
int first_line, last_line;
int first_column, last_column;
};
Thus, to get the starting line number of the third component,
use `@3.first_line'.
In order for the members of this structure to contain valid
information, you must make `yylex' supply this information about
each token. If you need only certain members, then `yylex' need
only fill in those members.
The use of this feature makes the parser noticeably slower.
File: bison.info, Node: Algorithm, Next: Error Recovery, Prev: Interface, Up: Top
The Algorithm of the Bison Parser
*********************************
As Bison reads tokens, it pushes them onto a stack along with their
semantic values. The stack is called the "parser stack". Pushing a
token is traditionally called "shifting".
For example, suppose the infix calculator has read `1 + 5 *', with a
`3' to come. The stack will have four elements, one for each token
that was shifted.
But the stack does not always have an element for each token read.
When the last N tokens and groupings shifted match the components of
a grammar rule, they can be combined according to that rule. This is
called "reduction". Those tokens and groupings are replaced on the
stack by a single grouping whose symbol is the result (left hand
side) of that rule. Running the rule's action is part of the process
of reduction, because this is what computes the semantic value of the
resulting grouping.
For example, if the infix calculator's parser stack contains this:
1 + 5 * 3
and the next input token is a newline character, then the last three
elements can be reduced to 15 via the rule:
expr: expr '*' expr;
Then the stack contains just these three elements:
1 + 15
At this point, another reduction can be made, resulting in the single
value 16. Then the newline token can be shifted.
The parser tries, by shifts and reductions, to reduce the entire
input down to a single grouping whose symbol is the grammar's
start-symbol (*note Language and Grammar::.).
This kind of parser is known in the literature as a bottom-up parser.
* Menu:
* Look-Ahead:: Parser looks one token ahead when deciding what to do.
* Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
* Precedence:: Operator precedence works by resolving conflicts.
* Contextual Precedence:: When an operator's precedence depends on context.
* Parser States:: The parser is a finite-state-machine with stack.
* Reduce/Reduce:: When two rules are applicable in the same situation.
File: bison.info, Node: Look-Ahead, Next: Shift/Reduce, Prev: Algorithm, Up: Algorithm
Look-Ahead Tokens
=================
The Bison parser does *not* always reduce immediately as soon as the
last N tokens and groupings match a rule. This is because such a
simple strategy is inadequate to handle most languages. Instead,
when a reduction is possible, the parser sometimes ``looks ahead'' at
the next token in order to decide what to do.
When a token is read, it is not immediately shifted; first it becomes
the "look-ahead token", which is not on the stack. Now the parser
can perform one or more reductions of tokens and groupings on the
stack, while the look-ahead token remains off to the side. When no
more reductions should take place, the look-ahead token is shifted
onto the stack. This does not mean that all possible reductions have
been done; depending on the token type of the look-ahead token, some
rules may choose to delay their application.
Here is a simple case where look-ahead is needed. These three rules
define expressions which contain binary addition operators and
postfix unary factorial operators (`!'), and allow parentheses for
grouping.
expr: term '+' expr
| term
;
term: '(' expr ')'
| term '!'
| NUMBER
;
Suppose that the tokens `1 + 2' have been read and shifted; what
should be done? If the following token is `)', then the first three
tokens must be reduced to form an `expr'. This is the only valid
course, because shifting the `)' would produce a sequence of symbols
`term ')'', and no rule allows this.
If the following token is `!', then it must be shifted immediately so
that `2 !' can be reduced to make a `term'. If instead the parser
were to reduce before shifting, `1 + 2' would become an `expr'. It
would then be impossible to shift the `!' because doing so would
produce on the stack the sequence of symbols `expr '!''. No rule
allows that sequence.
The current look-ahead token is stored in the variable `yychar'.
*Note Action Features::.
File: bison.info, Node: Shift/Reduce, Next: Precedence, Prev: Look-Ahead, Up: Algorithm
Shift/Reduce Conflicts
======================
Suppose we are parsing a language which has if-then and if-then-else
statements, with a pair of rules like this:
i